<?php
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$bannerTable = 'hellozjx_mapwxapp_banner';

if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $list = pdo_fetchall("SELECT id,name,url,sort,update_time,pic_img FROM " . tablename($bannerTable) .  " ORDER BY sort DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($bannerTable));
    $pager = pagination($total, $pindex, $psize);
} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    if (checksubmit('submit')) {
        if(empty($_GPC['name']) || empty($_GPC['pic_img']))
            message('必填项不能为空！', $this->createWebUrl('banner', array('op' => 'display')), 'error');

        $data = array(
            'uniacid' => $_W['uniacid'],
            'sort' => intval($_GPC['sort']),
            'name' => $_GPC['name'],
            'url' => $_GPC['url'],
            'pic_img' => $_GPC['pic_img'],
        );
        if (!empty($id)) {  //添加、修改教师信息表
            $data['update_time'] = date('Y-m-d H:i:s',time());
            pdo_update($bannerTable, $data, array('id' => $id));
        } else {
            $data['create_time'] = date('Y-m-d H:i:s',time());
            pdo_insert($bannerTable, $data);
            $id = pdo_insertid();
            if(!$id){
                message('操作失败！', $this->createWebUrl('banner', array('op' => 'display')), 'error');
            }
        }
        message('更新信息成功！', $this->createWebUrl('banner', array('op' => 'display')), 'success');
    }

    $banner = pdo_fetch("select * from ".tablename($bannerTable)." where id={$id}");
}elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $banner = pdo_fetch("SELECT id FROM " . tablename($bannerTable) . " WHERE id = '{$id}'");
    if (empty($banner)) {
        message('数据有误', $this->createWebUrl('banner', array('op' => 'display')), 'error');
    }
    pdo_delete($bannerTable, array('id' => $id));
    message('删除成功', referer(), 'success');
}
include $this->template('banner');